package yuyicman.display 
{
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import yuyicman.debug.Debugger;
	import yuyicman.utils.StringUtils;
	
	/**
	 * ...
	 * @author Yuyic.
	 */
	public class YYTextField extends TextField 
	{
		
		public function YYTextField() 
		{
			
		}
		
		
		private static var _tfkeys:Dictionary = new Dictionary( false );
		
		public static function randomTypingDisplay( tf:TextField,text:String,spd:Number=10,displayAsHtml:Boolean = false, textFormat:TextFormat = null ):void {
			
			if ( text.length ) {
				
				if ( _tfkeys[ tf ] ) {
					_tfkeys[ tf ].originalText = text;
					_tfkeys[ tf ].displayAsHtml = displayAsHtml;
					_tfkeys[ tf ].textFormat = textFormat;
					_tfkeys[ tf ].change();
				}
				
				else {
					
					var key:Object = {
						
						timer:new Timer( spd ),
						
						tf:tf,
						
						textFormat:textFormat,
						
						displayAsHtml:displayAsHtml,
						
						text: displayAsHtml ? StringUtils.stripTags(text) : text,
						
						originalText:text,
						
						i:0,
						
						_i:0,
						
						change:function():void {
							this.text = this.displayAsHtml ? StringUtils.stripTags(this.originalText) : this.originalText;
							this.changed = true;
							this.i = 0;
							this._i = this.tf.text.length;
							
						},
						
						start:function():void {
							if ( this.tf.text.length > 0 ) {
								this.change();
							}
							this.timer.addEventListener( TimerEvent.TIMER_COMPLETE, this._complete );
							this.timer.addEventListener( TimerEvent.TIMER, this._timer2 );
							this.timer.start();
							_tfkeys[ tf ] = this;
						},
						
						stop:function():void {
							if( this.displayAsHtml ){
								this.tf.htmlText = this.originalText;
							}
							if ( this.textFormat ) {
								this.tf.setTextFormat( key.textFormat );
							}
							this.timer.removeEventListener( TimerEvent.TIMER_COMPLETE, this._complete );
							this.timer.removeEventListener( TimerEvent.TIMER, this._timer2 );
							this.timer.stop();
							delete _tfkeys[ this.tf ];
						},
						
						_complete:function(e:TimerEvent):void {
							key.stop();
							e.updateAfterEvent();
						},
						
						_timer2:function(e:TimerEvent):void {
							
							
							if ( key.tf.text == key.text || !key.tf.parent ) {
								key.stop();
								return;
							}
							
							key.tf.text = key.text.substr( 0, key.i++ );
							
							
							
							key._i = Math.max( key._i -= 1, 0 );
							
							key.tf.appendText( String.fromCharCode.apply(key, function():Array {
								
								var _arr:Array = new Array;
								while ( _arr.length < key._i ) {
									
									// numerical only
									//_arr.push( Math.floor(Math.random() * 10) + 48 );
									_arr.push( Math.floor(Math.random() * 26) + 97 );
								}
								return _arr;
								
							}()) );
							
							/*if ( key.timer.currentCount % 4 == 0 ) {
								key.tf.appendText("_");
							}
							*/
							if ( key.textFormat ) {
								key.tf.setTextFormat( key.textFormat );
							}
							
							e.updateAfterEvent();
							
						}
						
						/*_timer:function(e:TimerEvent):void {
							
							
							if ( key.changed && (key.i -= 3) > 0 ) {
								key.i = Math.max( 0, key.i );
								key.tf.text = key.tf.text.substr( 0, key.i );
								return;
							}
							
							if ( key.changed ) {
								key.changed = false;
								key._i = key.i = 0;
							}
							
							if ( key.i++ == key.text.length-1 ) {
								key.stop();
							}
							
							key._i = Math.min( key.text.length - key.i, Math.min( key._i + 1, 3 ) );
							key.tf.text = key.text.substr( 0, key.i );
							
							
							key.tf.appendText( String.fromCharCode.apply(key, function():Array {
								
								var _arr:Array = new Array;
								
								while ( _arr.length < key._i ) {
									
									
									_arr.push( Math.floor(Math.random() * 26) + 97 );
									
								}
								return _arr;
								
							}()) );
							
							e.updateAfterEvent();
						}*/
						
					}
					_tfkeys[ tf ] = key;
					key.start();
				}
			}
			
		}
		
	}

}